#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<queue>
#include<functional>
using namespace std;
using pii = pair<int, int>;
const int N = 50010, M = 200010;
int h[N], e[M], ne[M], w[M], idx;
int source[6];
bool st[N];
int dist[6][N];
int n, m;
int res = 0x3f3f3f3f;
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra(int start, int dist[])
{
    memset(dist, 0x3f, N * 4);
    memset(st, false, sizeof st);
    dist[start] = 0;
    priority_queue<pii, vector<pii>, greater<pii> >q;
    q.push({ 0,start });
    while (q.size())
    {
        auto t = q.top();
        q.pop();
        int ver = t.second;
        if (st[ver]) continue;
        for (int i = h[ver]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                q.push({ dist[j],j });
            }
        }
        st[ver] = true;
    }
}
void dfs(int u, int prev, int d)
{
    if (u == 6)
    {
        res = min(res, d);
        return;
    }
    for (int i = 1; i <= 5; i++)
    {
        if (st[i]) continue;
        st[i] = true;
        dfs(u + 1, i, d + dist[prev][source[i]]);
        st[i] = false;
    }
}
int main()
{
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= 5; i++) cin >> source[i];
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    source[0] = 1;
    for (int i = 0; i <= 5; i++) dijkstra(source[i], dist[i]);
    memset(st, false, sizeof st);
    dfs(1, 0, 0);
    cout << res << endl;
    return 0;
}